<?php
/**
 *   OPENSHOP 客户统计
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: guest_stats.php,v 1.0 2009/06/19 09:36:58 modified $
 *   @author      FillBag <fillbag@hotmail.com>
 */

define('IN_OS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . 'includes/lib_order.php');

/* act操作项的初始化 */
if (empty($_REQUEST['act']))
{
    $_REQUEST['act'] = 'list';
}
else
{
    $_REQUEST['act'] = trim($_REQUEST['act']);
}

/*------------------------------------------------------ */
//-- 客户统计列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
    /* 权限判断 */
    admin_priv('veiw_client_flow_stats');

    /* 取得客户总数 */
    $users      =& init_users();
    $sql = "SELECT COUNT(*) FROM " . $os->table("users");
    $res = $db->getCol($sql);
    $user_num   = $res[0];


    /* 计算订单各种费用之和的语句 */
    $total_fee = " SUM(" . order_amount_field() . ") AS turnover ";

    /* 有过订单的客户数 */
    $sql = 'SELECT COUNT(DISTINCT user_id) FROM ' .$os->table('order_info').
           " WHERE user_id > 0 " . order_query_sql('finished');
    $have_order_usernum = $db->getOne($sql);

    /* 客户订单总数和订单总购物额 */
    $user_all_order = array();
    $sql = "SELECT COUNT(*) AS order_num, " . $total_fee.
           "FROM " .$os->table('order_info').
           " WHERE user_id > 0 " . order_query_sql('finished');
    $user_all_order = $db->getRow($sql);
    $user_all_order['turnover'] = floatval($user_all_order['turnover']);

    /* 匿名客户订单总数和总购物额 */
    $guest_all_order = array();
    $sql = "SELECT COUNT(*) AS order_num, " . $total_fee.
           "FROM " .$os->table('order_info').
           " WHERE user_id = 0 " . order_query_sql('finished');
    $guest_all_order = $db->getRow($sql);

    /* 匿名客户平均订单额: 购物总额/订单数 */
    $guest_order_amount = ($guest_all_order['order_num'] > 0) ? floatval($guest_all_order['turnover'] / $guest_all_order['order_num']) : '0.00';

    $_GET['flag'] = isset($_GET['flag']) ? 'download' : '';
    if($_GET['flag'] == 'download')
    {
	    /* 权限判断 */
	    admin_priv('down_client_flow_stats');
	    
        $filename = os_iconv(OS_CHARSET, 'GB2312', '客户统计报表');

        header("Content-type: application/vnd.ms-excel; charset=utf-8");
        header("Content-Disposition: attachment; filename=$filename.xls");

        /* 生成客户购买率 */
        $data  = "客户购买率 \t\n";
        $data .= "客户总数\t有订单客户数\t" .
                "客户订单总数\t客户购买率 \n";

        $data .= $user_num . "\t" . $have_order_usernum . "\t" .
                $user_all_order['order_num'] . "\t" . sprintf("%0.2f", ($user_num > 0 ? $have_order_usernum / $user_num : 0) * 100) . "\n\n";

        /* 每客户平均订单数及购物额 */
        $data .= "每客户平均订单数及购物额\t\n";

        $data .= "客户购物总额\t每客户订单数\t每客户购物额\n";

        $ave_user_ordernum = $user_num > 0 ? sprintf("%0.2f", $user_all_order['order_num'] / $user_num) : 0;
        $ave_user_turnover = $user_num > 0 ? price_format($user_all_order['turnover'] / $user_num) : 0;

        $data .= price_format($user_all_order['turnover']) . "\t" . $ave_user_ordernum . "\t" . $ave_user_turnover . "\n\n";

        /* 每客户平均订单数及购物额 */
        $data .= "匿名客户平均订单额及购物总额\t\n";
        $data .= "匿名客户购物总额\t匿名客户订单总数\t匿名客户平均订单额\n";

        $order_num = $guest_all_order['order_num'] > 0 ? price_format($guest_all_order['turnover'] / $guest_all_order['order_num']) : 0;
        $data .= price_format($guest_all_order['turnover']) . "\t" . $guest_all_order['order_num'] . "\t" .
                $order_num;

        echo os_iconv(OS_CHARSET, 'GB2312', $data) . "\t";
        exit;
    }

    /* 赋值到模板 */
    $smarty->assign('user_num',            $user_num);                    // 客户总数
    $smarty->assign('have_order_usernum',  $have_order_usernum);          // 有过订单的客户数
    $smarty->assign('user_order_turnover', $user_all_order['order_num']); // 客户总订单数
    $smarty->assign('user_all_turnover',   price_format($user_all_order['turnover']));  //客户购物总额
    $smarty->assign('guest_all_turnover',  price_format($guest_all_order['turnover'])); //匿名客户购物总额
    $smarty->assign('guest_order_num',     $guest_all_order['order_num']);              //匿名客户订单总数

    /* 每客户订单数 */
    $smarty->assign('ave_user_ordernum',  $user_num > 0 ? sprintf("%0.2f", $user_all_order['order_num'] / $user_num) : 0);

    /* 每客户购物额 */
    $smarty->assign('ave_user_turnover',  $user_num > 0 ? price_format($user_all_order['turnover'] / $user_num) : 0);

    /* 注册客户购买率 */
    $smarty->assign('user_ratio', sprintf("%0.2f", ($user_num > 0 ? $have_order_usernum / $user_num : 0) * 100));

     /* 匿名客户平均订单额 */
    $smarty->assign('guest_order_amount', $guest_all_order['order_num'] > 0 ? price_format($guest_all_order['turnover'] / $guest_all_order['order_num']) : 0);

    $smarty->assign('all_order',          $user_all_order);    //所有订单总数以及所有购物总额
    $smarty->assign('ur_here',            '客户统计');

    $smarty->assign('action_link',  array('text' => '客户统计报表下载',
          'href'=>'guest_stats.php?flag=download'));

    assign_query_info();
    $from = trim($_REQUEST['from']);
    if ($from == '')
    {
        $from = trim($_POST['from']);
    }
    $smarty->assign('from',$from);
    
    $smarty->display("guest_stats.tpl");   
}
?>